package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname DishController
 * @Description TODO
 * @Date 2023/3/5 11:25
 * @Created by 11404
 */


@Api(tags = "菜品管理")
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;


    //菜品分页查询
    @ApiOperation(value = "分页查询菜品")
    @Cacheable(value = "dish",key = "#name")
    @GetMapping("/page")
    public ResultInfo dishSelect(@RequestParam(value = "page", defaultValue = "1") Integer pageName,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                 String name
    ) {

        //这个Page<>类型是MP自带的分页类型
        Page<Dish> dishList = dishService.dhService(pageName, pageSize, name);

        return ResultInfo.success(dishList);

    }

    // 新增菜品  新增之前需要在菜品分类里加上根据ID查询菜品分类，因为页面里有个分类的选项功能
    @ApiOperation(value = "新增菜品")

    @PostMapping
    public ResultInfo save(@RequestBody Dish dish) {

        dishService.dishInsert(dish);

        return ResultInfo.success();
    }

    //修改菜品信息  这里还需要先做回显
    //回显菜品数据  根据ID查询
    @ApiOperation(value = "根据ID查询菜品")
    @Cacheable(value = "dish",key = "#id")
    @GetMapping("/{id}")
    public ResultInfo findById(@PathVariable Long id) {

        return ResultInfo.success(dishService.dishById(id));
    }

    //修改菜品
    @ApiOperation(value = "修改菜品")
    @CacheEvict(value = "dish",allEntries = true)
    @PutMapping
    public ResultInfo update(@RequestBody Dish dish) {
        dishService.updateDish(dish);
        return ResultInfo.success();
    }

    //菜品起售、停售
    //@RequestParam List<Long> ids  这里传集合
    @ApiOperation(value = "菜品的起售与停售")
    @CacheEvict(value = "dish",allEntries = true)
    @PostMapping("/status/{status}")
    public ResultInfo update(@PathVariable("status") Integer status,
                             @RequestParam List<Long> ids) {
        dishService.updateStatus(status, ids);

        return ResultInfo.success();
    }

    //菜品的删除
    @ApiOperation(value = "删除菜品")
    @CacheEvict(value = "dish",allEntries = true)
    @DeleteMapping
    public ResultInfo deleteDish(@RequestParam List<Long> ids) {

        dishService.deleteDishById(ids);

        return ResultInfo.success();
    }

    // 根据分类查询菜品列表
    @ApiOperation(value = "根据分类查询菜品列表")
    @Cacheable(value = "dish",key = "#categoryId")
    @GetMapping("/dish/list")
    public ResultInfo findListBySetmeal(Long categoryId,String name) {// 1.接收参数
        // 2.调用service
        List<Dish> dishList = dishService.findListBySetmeal(categoryId,name);
        // 3.返回结果
        return ResultInfo.success(dishList);
    }


}
